package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmall.passport.pojo.dto.AdminLoginDTO;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.passport.security.LoginPrincipal;
import cn.tedu.csmall.passport.service.impl.AdminServiceImpl;
import cn.tedu.csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "01. 管理员管理模块")
@Validated
@RestController
@RequestMapping("/admins")
@Slf4j
public class AdminController {
    public AdminController() {
        System.out.println("创建controller类：AdminController");
    }

    @Autowired
    AdminServiceImpl service;

    // http://localhost:9081/admins/login
    @ApiOperation("登录管理员")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String > login(AdminLoginDTO adminLoginDTO){
        log.debug("开始处理管理员登录请求，参数：{}",adminLoginDTO);
        String jwt = service.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }

    // http://localhost:9081/admins/add-new?username=张四&phone=123&email=123@qq.com
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO){
        log.debug("开始处理添加管理员请求，参数：{}",adminAddNewDTO);
        service.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/9/delete
    @ApiOperation("删除管理员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "管理员id",required = true, example = "10")
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    public JsonResult<Void> deleteById(@PathVariable Long id, @ApiIgnore @AuthenticationPrincipal LoginPrincipal principal){
        log.debug("开始处理删除管理员请求，参数：id={}",id);
        log.debug("当前登录的当事人：{}",principal);
        service.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/9/enable
    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id",value = "管理员id",required = true, dataType = "long", example = "40")
    @PostMapping("/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult<Void> setEnable(@PathVariable Long id){
        log.debug("开始处理启用【id={}管理员】请求",id);
        service.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/9/disable
    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 301)
    @ApiImplicitParam(name = "id",value = "管理员id",required = true, dataType = "long", example = "40")
    @PostMapping("/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult<Void> setDisable(@PathVariable Long id){
        log.debug("开始处理禁用【id={}管理员】请求",id);
        service.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<List<AdminListItemVO>> list(){
        log.debug("开始处理查询管理员列表请求");
        return JsonResult.ok(service.list());
    }

}
